package com.fnzn.entranceGuard.service.ths.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fnzn.entranceGuard.entity.power.PowerDay;
import com.fnzn.entranceGuard.entity.power.PowerHour;
import com.fnzn.entranceGuard.entity.ths.ThsBase;
import com.fnzn.entranceGuard.entity.ths.ThsDay;
import com.fnzn.entranceGuard.entity.ths.ThsHour;
import com.fnzn.entranceGuard.mapper.ths.ThsBaseMapper;
import com.fnzn.entranceGuard.service.ths.IThsBaseService;
import com.fnzn.entranceGuard.service.ths.IThsDayService;
import com.fnzn.entranceGuard.service.ths.IThsHourService;
import com.fnzn.entranceGuard.util.DateTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.reflect.annotation.ExceptionProxy;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sxf
 * @since 2019-01-22
 */
@Service
public class ThsBaseServiceImpl extends ServiceImpl<ThsBaseMapper, ThsBase> implements IThsBaseService {
    @Autowired
    private IThsHourService hourService;
    @Autowired
    private IThsDayService dayService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uninUpdate(ThsBase base) {

        Integer year = DateTools.getCurrentYear();
        Integer month = DateTools.getCurrentMonth();
        Integer day = DateTools.getCurrentDay();
        Integer hour = DateTools.getCurrentHour();
        String date = DateTools.getCurrentSysData(DateTools.SIMPLE_FORMAT);

        /**
         * 更新小时表
         */
        ThsHour thsHour = hourService.getByDateHour(base.getDeviceId(), date, hour,base.getPowerId());
        if (thsHour == null) {
            thsHour = new ThsHour();
            thsHour.setDate(date);
            thsHour.setHour(hour);
            thsHour.setPowerId(base.getPowerId());
            thsHour.setDeviceId(base.getDeviceId());
            thsHour.setHum(base.getHum());
            thsHour.setMaxHum(base.getHum());
            thsHour.setMinHum(base.getHum());
            thsHour.setTem(base.getTem());
            thsHour.setMaxTem(base.getTem());
            thsHour.setMinTem(base.getTem());
            hourService.insert(thsHour);
        } else {
            thsHour.setHum(base.getHum().add(thsHour.getHum()==null?new BigDecimal(0):thsHour.getHum()).divide(new BigDecimal(2),2,BigDecimal.ROUND_HALF_UP));
            if(base.getHum().compareTo(thsHour.getMaxHum()==null?new BigDecimal(0):thsHour.getMaxHum())>0){
                thsHour.setMaxHum(base.getHum());
            }
            if(base.getHum().compareTo(thsHour.getMinHum()==null?new BigDecimal(0):thsHour.getMinHum())<0){
                thsHour.setMinHum(base.getHum());
            }
            thsHour.setTem(base.getTem().add(thsHour.getTem()==null?new BigDecimal(0):thsHour.getTem()).divide(new BigDecimal(2),2,BigDecimal.ROUND_HALF_UP));
            if(base.getTem().compareTo(thsHour.getMaxTem()==null?new BigDecimal(0):thsHour.getMaxTem())>0){
                thsHour.setMaxTem(base.getTem());
            }
            if(base.getTem().compareTo(thsHour.getMinTem()==null?new BigDecimal(0):thsHour.getMinTem())<0){
                thsHour.setMinTem(base.getTem());
            }
            thsHour.setUpdateTime(new Date());
            hourService.updateById(thsHour);
        }
        /**
         * 更新日表
         */
        ThsDay thsDay = dayService.getByDeviceDate(base.getDeviceId(), year, month, day,base.getPowerId());
        if (thsDay == null) {
            thsDay = new ThsDay();
            thsDay.setDeviceId(base.getDeviceId());
            thsDay.setPowerId(base.getPowerId());
            thsDay.setDay(day);
            thsDay.setYear(year);
            thsDay.setMonth(month);
            thsDay.setHumAve(base.getHum());
            thsDay.setTemAve(base.getTem());
            dayService.insert(thsDay);
        } else {
            thsDay.setHumAve(base.getHum().add(thsDay.getHumAve()==null?new BigDecimal(0):thsDay.getHumAve()).divide(new BigDecimal(2),2,BigDecimal.ROUND_HALF_UP));
            thsDay.setTemAve(base.getTem().add(thsDay.getTemAve()==null?new BigDecimal(0):thsDay.getTemAve()).divide(new BigDecimal(2),2,BigDecimal.ROUND_HALF_UP));
            thsDay.setUpdateTime(new Date());
            thsDay.setUpdateTime(new Date());
            dayService.updateById(thsDay);
        }
        this.insert(base);
    }

    public static void main(String[] args) {
        BigDecimal b= null;
        BigDecimal c= new BigDecimal(12);
        BigDecimal d= new BigDecimal(2);
        System.out.println(c.add(b).divide(d));
    }
}
